package com.sz.biz.common.base.service.impl;

import com.sz.biz.common.constants.ErrorCodes;
import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.common.base.entity.FeeType;
import com.sz.biz.common.base.service.FeeTypeService;
import com.sz.common.base.cache.EntityCache;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.core.service.PrincipalLogUtils;
import com.sz.common.core.service.PrincipalUtils;
import com.sz.common.core.service.aop.JudgeAspect;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Function: 费用类型serviceImp <br>
 * Author: hpeng.wang <br>
 * Date: 2016-12-01 14:43:00
 */
@Service("feeTypeService")
public class FeeTypeServiceImpl extends AbstractService implements FeeTypeService {
    @Override
    protected String getMapperNamespace() {
        return "FeeTypeMapper";
    }

    @Override
    public void refreshCache() {
        EntityCache.clearNamespace(getMapperNamespace());
    }

    @JudgeAspect
    @Override
    public int save(FeeType entry) {
        dao.save(getSqlName("insertSelective"), entry);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_FEE_TYPE", entry);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_FEE_TYPE, UserActions.ADD, "新增费用类型", dataBackup);
        return entry.getId();
    }

    @Override
    public int delete(Integer id) {
        FeeType entry = this.findById(id);
        if (entry == null) {
            throw Exceptions.bizException(ErrorCodes.ERROR_OBJECT_EMPTY);
        }
        entry.setIsDeleted(true);
        entry.setLastUpdateTime(new Date());
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("DELETE_FEE_TYPE", id);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_FEE_TYPE, UserActions.DELETE, "删除费用类型", dataBackup);
        return this.update(entry);
    }

    @Override
    public void batchdelete(List<Integer> ids) {
        List<FeeType> list = findByIds(ids);
        if (list.size() > 0) {
            for (FeeType entry : list) {
                entry.setIsDeleted(true);
                entry.setLastUpdateTime(new Date());
            }
            batchUpdate(list);
            //保存操作日志
            Map<String, Object> dataBackup = new HashMap<>();
            dataBackup.put("DELETE_FEE_TYPE", ids);
            PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_FEE_TYPE, UserActions.DELETE, "删除费用类型", dataBackup);
        }
    }

    @JudgeAspect
    @Override
    public int update(FeeType entry) {
        int result = (int) dao.update(getSqlName("updateByPrimaryKey"), entry);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("UPDATE_FEE_TYPE", entry);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_FEE_TYPE, UserActions.UPDATE, "修改费用类型", dataBackup);
        return result;
    }


    @Override
    public void batchUpdate(List<FeeType> list) {
        //数据处理
        dao.batchUpdate(getSqlName("updateByPrimaryKey"), list);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("UPDATE_FEE_TYPE", list);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_FEE_TYPE, UserActions.UPDATE, "修改费用类型", dataBackup);
    }


    @Override
    public FeeType findById(Integer id) {
        ParamData pd = new ParamData();
        pd.put("id", id);
        FeeType entry = (FeeType) dao.findForObject(getSqlName("findById"), pd);
        return entry;
    }

    @Override
    public List<FeeType> findByIds(List<Integer> ids) {
        if (ids == null || ids.size() <= 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY);
        }
        ParamData pd = new ParamData();
        pd.put("ids", ids);
        List<FeeType> list = dao.findForList(getSqlName("findByIds"), pd, FeeType.class);
        return list;
    }


    @Override
    public QResultDto findByParam(QueryParams params) {
        ParamData pd = this.convertQueryParams(params);
        pd.put("language", PrincipalUtils.getLocalLanguage());
        List<Map> list = dao.findForList(getSqlName("findByParamListPage"), pd, Map.class);
        return new QResultDto(list, pd.getPagination());
    }

    @Override
    public QResultDto findI18nByParam(QueryParams params) {
        ParamData pd = this.convertQueryParams(params);
        List list = dao.findForList(getSqlName("findI18nByParamListPage"), pd, Map.class);
        return new QResultDto(list, pd.getPagination());
    }

    @Override
    public List<Integer> findType() {
        List<Integer> list = dao.findForList(getSqlName("findType"), null, Integer.class);
        return list;
    }

    @Override
    public void feeTypeCheckCode(int sid, String code) {
        ParamData pd = new ParamData();
        pd.put("id", sid);
        pd.put("code", code);
        int count = (Integer) dao.findForObject(getSqlName("findCountByCode"), pd);
        if (count > 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_MODEL_CODE_EXIST, "");
        }
    }


}
